package com.cuts.videoedit.widget

import android.content.Context
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.cuts.videoedit.bean.VideoClip
import com.cuts.videoedit.data.VideoFrameData
import com.cuts.videoedit.helper.IAvFrameHelper
import com.cuts.videoedit.util.dp2px
import kotlin.text.toInt

class VideoFrameRecyclerView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): RecyclerView(context, attrs, defStyleAttr) {
    var frameForTime = 0L
    private val frameWidth by lazy(LazyThreadSafetyMode.NONE) { context.dp2px(48f).toInt() }


    var videoData: List<VideoClip>? = null


    val listData = mutableListOf<VideoFrameData>()

    private var videoFrameItemDecoration: VideoFrameItemDecoration?=null
//    var mIAvFrameHelper:IAvFrameHelper?=null;

    private val decorationWidth by lazy(LazyThreadSafetyMode.NONE) { context.dp2px(2f).toInt() }
    val halfDurationSpace = decorationWidth / 2
    /**
     * 通过x坐标找到对应的view
     */
    private fun findChildViewByX(x: Float): View? {
        val findChildViewUnder = findChildViewUnder(x, height / 2f)
        findChildViewUnder?.let {
            return it
        }

        for (i in 0 until childCount) {
            val child = getChildAt(i)
            val position = getChildAdapterPosition(child)
            if (position !in 0 until listData.size) {
                continue
            }
            val item = listData[position]
            val left = if (item.isFirstItem && position > 0) {
                child.left - halfDurationSpace
            } else {
                child.left
            }

            val right = if (item.isLastItem && position < listData.size - 1) {
                child.right + halfDurationSpace
            } else {
                child.right
            }

            if (left <= x && x <= right) {
                return child
            }
        }

        return null
    }
    /**
     * 当前游标指定的View
     * 用时间去找，比用坐标找更精确！可以精确到1ms,坐标只能精确到1px
     */
    private fun getCurrentCursorView(): View? {
        return findChildViewByX(paddingLeft.toFloat())
    }
    fun getCurrentCursorVideoRect(rect: RectF) {
        val child = getCurrentCursorView() ?: return
    }


    fun getAvFrameHelper():IAvFrameHelper?{
        return (adapter as VideoFrameAdapter).getAvframeHelper()
    }

    fun setAvFrameHelper(helper: IAvFrameHelper){
        helper.init()
        helper.itemFrameForTime = frameForTime
        (adapter as VideoFrameAdapter).setAvframeHelper(helper)
    }

    public fun rebindFrameInfo() {
        listData.clear()

    }

    init {
        adapter= VideoFrameAdapter(listData, frameWidth)
        videoFrameItemDecoration = VideoFrameItemDecoration(context)
        addItemDecoration(videoFrameItemDecoration!!)
        addOnScrollListener(object :OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }
        })
    }



}


